<?php

class Application_Model_FeedEntry
{
    public $id = -1;
    public $rss_entry_id = 0;
    public $feed = null;
    public $title;
    public $link;
    public $description;
    public $author;
    public $publish_time;
    public $enclosure;

    public static function fromTableRow($feed_entry)
    {
        $result = new Application_Model_FeedEntry();
        $result->id = (int)$feed_entry['feed_entry_id'];
        $result->title = $feed_entry['title'];
        $result->link = $feed_entry['link'];
        $result->description = $feed_entry['description'];
        $result->author = $feed_entry['author'];
        $result->publish_time = $feed_entry['publish_time'];
        $result->enclosure = $feed_entry['enclosure'];
        return $result;
    }
    static $mime_types = array('image/gif' => true, 'image/jpeg' => true, 'image/png' => true, 'image/tiff' => true);

    /**
     * Gets the feed entries from a feed
     * @param string $feedUrl The url of the feed
     * @param Zend_Date $minPublishTime The peremption date of the entries
     * @param int $max The max number of feeds
     * @return NULL|array: An array of feed entries, NULL if any exception occured.
     */
    public static function entriesFromFeed($feedUrl, $minPublishTime = null, $max = -1)
    {
        $result = array();
        try
        {
            $canal = Zend_Feed_Reader::import($feedUrl);
        }
        catch (Exception $e)
        {
            return null;
        }
        $i = 0;
        foreach ($canal as $entry)
        {
            if ($max > -1 && $i++ > $max)
                break;
            $dateModified = $entry->getDateModified() ? $entry->getDateModified() : Zend_Date::now();
            if ($minPublishTime && $minPublishTime->isLater($dateModified))
                break;
            $feedEntry = new Application_Model_FeedEntry();
            $feedEntry->rss_entry_id = $entry->getId();
            $feedEntry->title = (string)$entry->getTitle();
            $feedEntry->description = (string)$entry->getDescription();
            $feedEntry->publish_time = $dateModified->toValue(Zend_Date::TIMESTAMP);
            $authors = $entry->getAuthors();
            $authors = $authors[0];
            $feedEntry->author = (string)$authors['name'];
            $feedEntry->link = (string)$entry->getLink();
            $enclosure = $entry->getEnclosure();
            if ($enclosure != null)
            {
                $type = $enclosure->type;
                if (isset(self::$mime_types[(string)$type]))
                {
                    $feedEntry->enclosure = $enclosure->url;
                }
            }
            if (!$feedEntry->enclosure)
            {
                $desc = $feedEntry->description;
                $imgs = null;
                if (preg_match_all("|<img[^>]*src=[\"'](.*)[\"'][^>]*/?>|U", $desc, $imgs))
                {
                    $balise = $imgs[0][0];
                    $pos = strpos($balise, 'width="1"');
                    if ($pos === false)
                    {
                        $pos = strpos($balise, 'height="1"');
                    }
                    if ($pos === false)
                    {
                        $feedEntry->enclosure = isset($imgs[1][0]) ? $imgs[1][0] : null;
                    }
                    else
                    {
                        $feedEntry->enclosure = null;
                    }
                }
            }
            array_push($result, $feedEntry);
        }
        return $result;
    }

    public function __destruct()
    {
        unset($this->id);
        unset($this->feed);
        unset($this->title);
        unset($this->link);
        unset($this->description);
        unset($this->author);
        unset($this->publish_time);
        unset($this->enclosure);
    }
}

